package cc.ufinity.networkdiagnose.view;

import java.util.List;
import java.util.Map;

import cc.ufinity.networkdiagnose.manager.MessageWhat;
import cc.ufinity.networkdiagnose.manager.WlanManager;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public final class HomeActivity extends ListActivity {
	private WlanManager wlanManager;
	Handler wifiReceiverHandler;
	LinearLayout wifiListLayout;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		wifiListLayout = (LinearLayout) this
				.findViewById(R.id.wifi_list_layout);
		wlanManager = new WlanManager(this);
		// 设置扫描WLAN热点句柄
		wifiReceiverHandler = new Handler() {
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case MessageWhat.WIFI_RECEIVE_LIST:
					update();
					break;
				}
			}
		};

	}

	private void update() {
		List<Map<String, Object>> data = wlanManager.getWifiList();
		SimpleAdapter adapter = new SimpleAdapter(this, data,
				R.layout.wifi_list_widget, new String[] { "ssid", "mac", "img",
						"level" }, new int[] { R.id.wifi_ssid, R.id.wifi_mac,
						R.id.wifi_img, R.id.wifi_level });
		setListAdapter(adapter);
	}

	@Override
	protected void onResume() {
		super.onResume();
		startWifiScan();
	}

	@Override
	protected void onPause() {
		super.onPause();

		wlanManager.stopScan();
	}

	void startWifiScan() {
		Toast.makeText(this, "开始搜索CMCC网络.", 5000).show();
		wlanManager.setHandler(wifiReceiverHandler);
		wlanManager.startScan();
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (KeyEvent.KEYCODE_BACK == keyCode) {
			//showDialog();
			Log.d("返回 任务测试", "返回按钮");
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	protected void exit() {
		android.os.Process.killProcess(android.os.Process.myPid());
	}

	private void showDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示");
//        builder.setIcon(R.drawable.what);
		builder.setMessage("确定退出系统吗？");

		builder.setCancelable(false);
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int id) {
				exit();
			}

		});
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				dialog.cancel();
			}
		});
		builder.show();
	}
	
	// *添加菜单
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
	//	menu.add(0, 0, 0, "").setIcon(R.drawable.xml_main_exit);
		menu.add(0, 0, 0, "退    出");

		return super.onCreateOptionsMenu(menu);
	}

	// *管理菜单项
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		if (item.getItemId() == 0) {
			showDialog();
		}

		return super.onOptionsItemSelected(item);
	}
}